/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QKeyEvent>
#include "query.h"

#include "dateedit.h"
using namespace SQL;

DateEdit::DateEdit(QWidget *parent)
		: QDateTimeEdit(parent),m_query(0),m_handleFocus(true),m_setCurrentDateOnInsert(false)
{
	setDisplayFormat( "dd.MM.yyyy" );
	connect(this,SIGNAL(editingFinished()),SLOT(bindValue()));
}

DateEdit::~DateEdit()
{
}


bool DateEdit::handleFocus()
{
	return m_handleFocus;
}

void DateEdit::setHandleFocus(bool handleFocus)
{
	m_handleFocus=handleFocus;
}

bool DateEdit::setCurrentDateOnInsert()
{
	return m_setCurrentDateOnInsert;
}
void DateEdit::setSetCurrentDateOnInsert(bool setCurrentDateOnInsert)
{
	m_setCurrentDateOnInsert=setCurrentDateOnInsert;
}

const QString & DateEdit::queryName()
{
	return m_queryName;
}

void DateEdit::setQueryName(const QString & queryName)
{
	m_queryName = queryName;
	m_query = defaultDatabase()->query(m_queryName);

	if (!m_query)
		return;
	connect(m_query, SIGNAL(recordChanged(const QSqlRecord&)), SLOT(recordChanged(const QSqlRecord&)));
	connect(m_query, SIGNAL(beforeInsert()), SLOT(setReadWrite()));
	connect(m_query, SIGNAL(beforeUpdate()), SLOT(setReadWrite()));
	connect(m_query, SIGNAL(afterSelect()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterClose()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterPost()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterCancel()), SLOT(setReadOnly()));
	setDisabled(m_query->state() == Query::Select);
	if (m_fieldName.length())
		setFieldName(m_fieldName);
}

const QString & DateEdit::fieldName()
{
	return m_fieldName;
}

void DateEdit::setFieldName(const QString & fieldName)
{
	m_fieldName = fieldName;
	if (m_query)
	{
		setDateTime(m_query->value(m_fieldName).toDateTime());
		setAlignment(Query::typeIsNumber(m_query->value(m_fieldName).type()) ? Qt::AlignRight : Qt::AlignLeft);
	}
}

void DateEdit::setReadWrite()
{
	setEnabled(true);
	if (!m_query || !m_fieldName.length())
		return;
	if (m_query->state()==Query::Insert)
	{
		if (m_setCurrentDateOnInsert)
			setDateTime( QDateTime::currentDateTime() );
		else
			clear();
	}
	m_query->setValue(m_fieldName,text());
}

void DateEdit::focusInEvent(QFocusEvent * event)
{
	if (m_query && m_query->state() == Query::Update)
		selectAll();

	QDateTimeEdit::focusInEvent(event);
	if (event->isAccepted())
		emit(focusIn());
}

void DateEdit::focusOutEvent(QFocusEvent * event)
{
	QDateTimeEdit::focusOutEvent(event);
	if (event->isAccepted())
		emit(focusOut());
}


void DateEdit::setReadOnly()
{
	setDisabled(true);
	setAlignment(Query::typeIsNumber(m_query->value(m_fieldName).type()) ? Qt::AlignRight : Qt::AlignLeft);
	if (!m_query || !m_fieldName.length())
		return;
	if (m_query->state()==Query::Select)
		setDateTime(m_query->value(m_fieldName).toDateTime());
}

void DateEdit::recordChanged(const QSqlRecord& record)
{
	setDateTime(record.value(m_fieldName).toDateTime());
}

void DateEdit::bindValue()
{
	if (!m_query || !m_fieldName.length())
		return;
	m_query->setValue(m_fieldName,dateTime().toString(Qt::ISODate));
}

void DateEdit::keyPressEvent(QKeyEvent * event)
{
	if (event->key()==Qt::Key_Escape)
	{
		if (m_handleFocus)
			focusPreviousChild();
		emit(escapePressed());
	}
	if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter)
	{
		bindValue();
		if (m_handleFocus)
			focusNextChild();
		emit(returnPressed());
	}
	QDateTimeEdit::keyPressEvent(event);
}
